package com.chujian.system.core.controller;


import com.chujian.framework.component.EntityHelper;
import com.chujian.framework.component.EntityInfo;
import com.chujian.framework.core.base.BaseController;
import com.chujian.framework.core.base.BaseService;
import com.chujian.framework.core.tsdata.Result;
import com.chujian.core.entity.CoreClass;
import com.chujian.system.core.service.CoreClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 控制层
 *
 * @author chujian
 * @since 2021-08-24
 */
@RequestMapping("/coreClass")
@RestController
@Slf4j
public class CoreClassController extends BaseController<CoreClass> {

    @Resource
    private CoreClassService baseService;

    @Override
    public BaseService<CoreClass> getBaseService() {
        return baseService;
    }

    /**
     * 通过全类名，生成类信息
     *
     * @param coreClass coreClass
     * @return Result
     */
    @PostMapping("/saveEntityByFullClass")
    public Result<?> saveEntityByFullClass(@RequestBody CoreClass coreClass) throws Exception {
        EntityInfo info = EntityHelper.collectEntityInfo(Class.forName(coreClass.getClassName()));
        CoreClass rs = baseService.saveEntityInfo(info);
        return okRs(rs);
    }

    /**
     * 保存，传入 EntityInfo
     *
     * @param info entityInfo
     * @return Result
     */
    @PostMapping("/saveEntityInfo")
    public Result<?> saveEntityInfo(@RequestBody EntityInfo info) {
        CoreClass rs = baseService.saveEntityInfo(info);
        return okRs(rs);
    }

}
